﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UI;
using Base.Data;
using Base.Extensions;
using Base.Custom;
public class RelationshipPanel : BasePanel {
    List<RelationData> relaData;
    public Object systemPrefab;
    public Object relationPrefab;
    private Transform content;
    private Transform rlit;
    private bool IsEnetr = false;
    public override void OnEnter()
    {
        gameObject.SetActive(true);
    }
    public override void OnExit()
    {
        gameObject.SetActive(false);
    }
    void SetSystemItem(SystemJson json,Transform content)
    {
        Transform system = systemPrefab.InstanUI(content);
        system.Find("Title/Pipeline/Title").SetText(json.name);
        system.Find("Title/Toggle").SetToggleOnClick(delegate (bool ison)
        {
            if (ison)
            {
                foreach (var id in json.objid)
                {
                    Dictionaries.SingleObject.GetKeyValue<string, SingleObject>(id).go.SetActive(true);
                }
            }
            else
            {
                foreach (var id in json.objid)
                {
                    Dictionaries.SingleObject.GetKeyValue<string, SingleObject>(id).go.SetActive(false);
                }
            }
        });
        RelationData data = system.gameObject.AddComponent<RelationData>();
        data.code = json.code;
        system.Find("Title/Pipeline/Title").SetText(json.name);
        relaData.Add(data);
    }
    // Use this for initialization
    void Awake() 
    {
        rlit = GameObject.FindGameObjectWithTag("系统关系右键菜单").transform;
        rlit.gameObject.SetActive(false);
        relaData = new List<RelationData>();
        Transform shui = transform.Find("Windoe/Scroll View/Viewport/Shui");
        Transform feng = transform.Find("Windoe/Scroll View/Viewport/Feng");
        Transform dian = transform.Find("Windoe/Scroll View/Viewport/Dian");
        foreach (var _item in Dictionaries.sysjson)
        {
            foreach (var item in _item.shui)
            {
                SetSystemItem(item, shui);
            }
            foreach (var item in _item.feng)
            {
                SetSystemItem(item, feng);
            }
            foreach (var item in _item.dian)
            {
                SetSystemItem(item, dian);
            }
        }
        foreach (var item in Dictionaries.realtion)
        {
            string[] codeid= item.relationCode.Split('@');
            string str = "";
            for (int i = 0; i < codeid.Length; i++)
            {
                int index = relaData.FindIndex(s => s.code.Split('@')[s.code.Split('@').Length-1] == codeid[i]);
                if (index!=-1)
                {
                    content=relaData[index].transform.Find("Content");
                    str = codeid[i];
                }
                else
                {
                    Transform relat = relationPrefab.InstanUI(content);
                    RelationData data = relat.gameObject.AddComponent<RelationData>();
                    str += "@" + codeid[i];
                    data.code = str;
                    content= relat.transform.Find("Content");
                    RelationPath path = Dictionaries.relationpath.Find(s => s.code == codeid[i]);
                    relat.Find("Title/Pipeline/Title").SetText(path.pahtname);
                    relat.Find("Title/Valve/Title").SetText(path.kgname);
                    relaData.Add(data);
                }
            }
        }
    }
	// Update is called once per frame
	void Update () {
        if (rlit.gameObject.activeSelf&&IsEnetr)
        {
            if (Input.GetMouseButtonDown(0))
            {
                StartCoroutine(offRightMenu(0.2f));
            }
        }
	}
    IEnumerator offRightMenu(float item)
    {
        yield return new WaitForSeconds(item);
        rlit.gameObject.SetActive(false);
    }
   public void 右键事件(Vector3 鼠标位置, RelationData data)
    {
        rlit.position = 鼠标位置;
        
        rlit.gameObject.SetActive(true);
        rlit.GetComponent<RenlationRightMenu>().data = data;
        print(鼠标位置);
    }
    public void Enter()
    {
        IsEnetr = false;
    }
    public void Exit()
    {
        IsEnetr = true;
    }
}
